<?php

declare(strict_types=1);

namespace App\CustHub\Mapper;



use App\CustHub\Model\ChCase;
use Hyperf\Database\Model\Collection;
use Mine\Abstracts\AbstractMapper;
use Mine\Annotation\Transaction;
use Mine\Exception\NormalStatusException;
use Mine\MineModel;

class ChCaseMapper extends AbstractMapper
{
    public function assignModel()
    {
        $this->model = ChCase::class;
    }

    /**
     * 获取客户案例总列表
     * @param $params
     * @return array
     */
    public function getCaseList($params): array
    {
        $filters=[
            'brief'=>'like',
            'industry'=>'='
        ];
        $query = $this->model::query()
            ->select('*');
        $query = applyFilters($query, $params,$filters);
        $paginate = $query->paginate((int)$params['pageSize']??MineModel::PAGE_SIZE);
        return $this->setPaginate($paginate);
    }

    /**
     * 根据行业获取客户案例列表
     * @param $params
     * @return array
     */
    public function getCaseListByIndustry($params): array
    {
        $query = $this->model::query()
            ->select('ch_case.id', 'ch_case.image', 'ch_case.brief', 'ch_case.industry', 'ch_case_industry.content as industry_text')
            ->leftJoin('ch_case_industry', 'ch_case.industry', '=', 'ch_case_industry.industry');

        if (!empty($params['industry'])) {
            $query->where('ch_case.industry', $params['industry']);
        }

        $paginate = $query->paginate((int)$params['pageSize']??MineModel::PAGE_SIZE);

        return $this->setPaginate($paginate);
    }

    /**
     * 获取客户案例详情
     * @param $params
     * @return Collection
     */
    public function getCaseDetail($params)
    {
        $case = $this->model::query()
            ->where('id', '=', $params['id'])
            ->first();

        $case->increment('view');

        return $case;
    }

    public function delete($ids): bool
    {
        $delete = $this->model::query()
            ->whereIn('id', $ids)
            ->delete();
        if (!$delete) {
            throw new NormalStatusException("删除失败");
        }
        return true;
    }
}